// GoGOST -- Pure Go GOST cryptographic functions library
// Copyright (C) 2015-2024 Sergey Matveev <stargrave@stargrave.org>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package gost34112012512

import (
	"bytes"
	"testing"

	"golang.org/x/crypto/pbkdf2"
)

// Test vectors from http://tc26.ru/.../R_50.1.111-2016.pdf

func Test1(t *testing.T) {
	if !bytes.Equal(pbkdf2.Key(
		[]byte("password"),
		[]byte("salt"),
		1,
		64,
		New,
	), []byte{
		0x64, 0x77, 0x0a, 0xf7, 0xf7, 0x48, 0xc3, 0xb1,
		0xc9, 0xac, 0x83, 0x1d, 0xbc, 0xfd, 0x85, 0xc2,
		0x61, 0x11, 0xb3, 0x0a, 0x8a, 0x65, 0x7d, 0xdc,
		0x30, 0x56, 0xb8, 0x0c, 0xa7, 0x3e, 0x04, 0x0d,
		0x28, 0x54, 0xfd, 0x36, 0x81, 0x1f, 0x6d, 0x82,
		0x5c, 0xc4, 0xab, 0x66, 0xec, 0x0a, 0x68, 0xa4,
		0x90, 0xa9, 0xe5, 0xcf, 0x51, 0x56, 0xb3, 0xa2,
		0xb7, 0xee, 0xcd, 0xdb, 0xf9, 0xa1, 0x6b, 0x47,
	}) {
		t.FailNow()
	}
}

func Test2(t *testing.T) {
	if !bytes.Equal(pbkdf2.Key(
		[]byte("password"),
		[]byte("salt"),
		2,
		64,
		New,
	), []byte{
		0x5a, 0x58, 0x5b, 0xaf, 0xdf, 0xbb, 0x6e, 0x88,
		0x30, 0xd6, 0xd6, 0x8a, 0xa3, 0xb4, 0x3a, 0xc0,
		0x0d, 0x2e, 0x4a, 0xeb, 0xce, 0x01, 0xc9, 0xb3,
		0x1c, 0x2c, 0xae, 0xd5, 0x6f, 0x02, 0x36, 0xd4,
		0xd3, 0x4b, 0x2b, 0x8f, 0xbd, 0x2c, 0x4e, 0x89,
		0xd5, 0x4d, 0x46, 0xf5, 0x0e, 0x47, 0xd4, 0x5b,
		0xba, 0xc3, 0x01, 0x57, 0x17, 0x43, 0x11, 0x9e,
		0x8d, 0x3c, 0x42, 0xba, 0x66, 0xd3, 0x48, 0xde,
	}) {
		t.FailNow()
	}
}

func Test4096(t *testing.T) {
	if !bytes.Equal(pbkdf2.Key(
		[]byte("password"),
		[]byte("salt"),
		4096,
		64,
		New,
	), []byte{
		0xe5, 0x2d, 0xeb, 0x9a, 0x2d, 0x2a, 0xaf, 0xf4,
		0xe2, 0xac, 0x9d, 0x47, 0xa4, 0x1f, 0x34, 0xc2,
		0x03, 0x76, 0x59, 0x1c, 0x67, 0x80, 0x7f, 0x04,
		0x77, 0xe3, 0x25, 0x49, 0xdc, 0x34, 0x1b, 0xc7,
		0x86, 0x7c, 0x09, 0x84, 0x1b, 0x6d, 0x58, 0xe2,
		0x9d, 0x03, 0x47, 0xc9, 0x96, 0x30, 0x1d, 0x55,
		0xdf, 0x0d, 0x34, 0xe4, 0x7c, 0xf6, 0x8f, 0x4e,
		0x3c, 0x2c, 0xda, 0xf1, 0xd9, 0xab, 0x86, 0xc3,
	}) {
		t.FailNow()
	}
}

func TestWithZero(t *testing.T) {
	if !bytes.Equal(pbkdf2.Key(
		[]byte("pass\x00word"),
		[]byte("sa\x00lt"),
		4096,
		64,
		New,
	), []byte{
		0x50, 0xdf, 0x06, 0x28, 0x85, 0xb6, 0x98, 0x01,
		0xa3, 0xc1, 0x02, 0x48, 0xeb, 0x0a, 0x27, 0xab,
		0x6e, 0x52, 0x2f, 0xfe, 0xb2, 0x0c, 0x99, 0x1c,
		0x66, 0x0f, 0x00, 0x14, 0x75, 0xd7, 0x3a, 0x4e,
		0x16, 0x7f, 0x78, 0x2c, 0x18, 0xe9, 0x7e, 0x92,
		0x97, 0x6d, 0x9c, 0x1d, 0x97, 0x08, 0x31, 0xea,
		0x78, 0xcc, 0xb8, 0x79, 0xf6, 0x70, 0x68, 0xcd,
		0xac, 0x19, 0x10, 0x74, 0x08, 0x44, 0xe8, 0x30,
	}) {
		t.FailNow()
	}
}

func Test100(t *testing.T) {
	if !bytes.Equal(pbkdf2.Key(
		[]byte("passwordPASSWORDpassword"),
		[]byte("saltSALTsaltSALTsaltSALTsaltSALTsalt"),
		4096,
		100,
		New,
	), []byte{
		0xb2, 0xd8, 0xf1, 0x24, 0x5f, 0xc4, 0xd2, 0x92,
		0x74, 0x80, 0x20, 0x57, 0xe4, 0xb5, 0x4e, 0x0a,
		0x07, 0x53, 0xaa, 0x22, 0xfc, 0x53, 0x76, 0x0b,
		0x30, 0x1c, 0xf0, 0x08, 0x67, 0x9e, 0x58, 0xfe,
		0x4b, 0xee, 0x9a, 0xdd, 0xca, 0xe9, 0x9b, 0xa2,
		0xb0, 0xb2, 0x0f, 0x43, 0x1a, 0x9c, 0x5e, 0x50,
		0xf3, 0x95, 0xc8, 0x93, 0x87, 0xd0, 0x94, 0x5a,
		0xed, 0xec, 0xa6, 0xeb, 0x40, 0x15, 0xdf, 0xc2,
		0xbd, 0x24, 0x21, 0xee, 0x9b, 0xb7, 0x11, 0x83,
		0xba, 0x88, 0x2c, 0xee, 0xbf, 0xef, 0x25, 0x9f,
		0x33, 0xf9, 0xe2, 0x7d, 0xc6, 0x17, 0x8c, 0xb8,
		0x9d, 0xc3, 0x74, 0x28, 0xcf, 0x9c, 0xc5, 0x2a,
		0x2b, 0xaa, 0x2d, 0x3a,
	}) {
		t.FailNow()
	}
}

func Test16777216(t *testing.T) {
	t.Skip("takes too long")
	if !bytes.Equal(pbkdf2.Key(
		[]byte("password"),
		[]byte("salt"),
		16777216,
		64,
		New,
	), []byte{
		0x49, 0xe4, 0x84, 0x3b, 0xba, 0x76, 0xe3, 0x00,
		0xaf, 0xe2, 0x4c, 0x4d, 0x23, 0xdc, 0x73, 0x92,
		0xde, 0xf1, 0x2f, 0x2c, 0x0e, 0x24, 0x41, 0x72,
		0x36, 0x7c, 0xd7, 0x0a, 0x89, 0x82, 0xac, 0x36,
		0x1a, 0xdb, 0x60, 0x1c, 0x7e, 0x2a, 0x31, 0x4e,
		0x8c, 0xb7, 0xb1, 0xe9, 0xdf, 0x84, 0x0e, 0x36,
		0xab, 0x56, 0x15, 0xbe, 0x5d, 0x74, 0x2b, 0x6c,
		0xf2, 0x03, 0xfb, 0x55, 0xfd, 0xc4, 0x80, 0x71,
	}) {
		t.FailNow()
	}
}
